package com.moleq.device.printer;

import com.epson.eposprint.Builder;
import com.epson.eposprint.EposException;
import com.epson.eposprint.Print;
import com.epson.epsonio.IoStatus;

public class TV88MPrinter extends Printer {

	private String mIpAddress = "";
	private int mPort = 0;
//	private EpsonIo mEpsonPort = null;
//	private int mInterface;
	static Print printer = null;
	String printerName = "TM-T88V";
	int language = com.epson.eposprint.Builder.LANG_EN;
	int connectionType = Print.DEVTYPE_TCP;
	static final int SEND_TIMEOUT = 10 * 1000;
	private int lineSpacing = 30;
	
	public TV88MPrinter() {
		printer = new Print();
//		mInterface = DevType.TCP;

		lineWidth = 42;
	}

	public void setIpAddress(String ip, int port) {
		mIpAddress = ip;
		mPort = port;

	}
	
	@Override
	public void print(String buffer) {
		mIsBusy = true;

		Builder builder = null;
		String method = "";
		String error = "";
		try {
			// create builder

			method = "Builder";
			builder = new Builder(printerName, language);
			// add command
			method = "addTextFont";
			builder.addTextFont(getBuilderFont());

			method = "addTextAlign";
			builder.addTextAlign(getBuilderAlign());

			method = "addTextLineSpace";
			builder.addTextLineSpace(getBuilderLineSpace());

			method = "addTextLang";
			builder.addTextLang(getBuilderLanguage());

			method = "addTextSize";
			builder.addTextSize(getBuilderSizeW(), getBuilderSizeH());

			method = "addTextStyle";
			builder.addTextStyle(Builder.FALSE, getBuilderStyleUnderline(),getBuilderStyleBold(), Builder.COLOR_1);

			method = "addTextPosition";
			builder.addTextPosition(getBuilderXPosition());

			method = "addText";
			builder.addText(buffer);

			method = "addFeedUnit";
			builder.addFeedUnit(getBuilderFeedUnit());

			// send builder data
			int[] status = new int[1];
			try {
				// Print printer = EPOSPrintSampleActivity.getPrinter();
				printer.sendData(builder, SEND_TIMEOUT, status);
				// ShowMsg.showStatus(EposException.SUCCESS, status[0], this);
			} catch (EposException e) {
				// ShowMsg.showStatus(e.getErrorStatus(), e.getPrinterStatus(),
				// this);
			}
		} catch (Exception e) {
			// ShowMsg.showException(e, method, this);
		}

		// remove builder
		if (builder != null) {
			try {
				builder.clearCommandBuffer();
				builder = null;
			} catch (Exception e) {
				builder = null;
			}
		}

		mIsBusy = false;

	}
	
	@Override
	public void print() {
		String content = "";
		if (rows != null) {
			for (int i = 0; i < rows.count(); i++) {
				content += rows.get(i).getContent(this.lineWidth);
			}
			print(content);
		}
	}

	@Override
	public String getPrinterName() {
		// TODO Auto-generated method stub
		return Printer.EPSON_WIRELESS_PRINTER;
	}

	@Override
	public void open() {
		
        try{
            printer = new Print();
            printer.openPrinter(connectionType, mIpAddress);
            ResponseMsg = "OPEN SUCCESS";
            mIsAvailable = true;
            mEnable = true;
        }catch(Exception e){
            printer = null;
            mEnable = false;
            return;
        }
	}
	
//	public boolean connectIP() {
//		
////        try{
////           // printer = new Print();
////            printer.openPrinter(connectionType, mIpAddress);
////            ResponseMsg = "OPEN SUCCESS";
////        }catch(Exception e){
////            printer = null;
////            return false;
////        }
//		if (mEnable) {
//			return true;
//		}else {
//			return true;
//		}
//	}
	
	

	@Override
	public void close() {
		mIsAvailable = false;
		
		
		 try{
	            printer.closePrinter();
//	            printer = null;
	            ResponseMsg = "CLOSE SUCCESS";
	        }catch(Exception e){
	        	mEnable = false;
	            printer = null;
	        }
		 
		

	}

	@Override
	public void cutPaper() {
		 Builder builder = null;
	        String method = "";
	        try{
	            //create builder
	    
	            method = "Builder";
	            builder = new Builder(printerName,language);

	            //add command
	            method = "addCut";
	            builder.addCut(getBuilderType());

	            //send builder data
	            int[] status = new int[1];
	            try{
	                printer.sendData(builder, SEND_TIMEOUT, status);
	            }catch(EposException e){
//	                ShowMsg.showStatus(e.getErrorStatus(), e.getPrinterStatus(), this);
	            }
	        }catch(Exception e){
//	            ShowMsg.showException(e, method, this);
	        }
	        
	        //remove builder
	        if(builder != null){
	            try{
	                builder.clearCommandBuffer();
	                builder = null;
	            }catch(Exception e){
	                builder = null;
	            }
	        }
	}

	private void dispErrStatus(int errStatus) {
		switch (errStatus) {

		case IoStatus.ERR_PARAM:
			ResponseMsg = "PARAMETER ERROR";
			break;

		case IoStatus.ERR_OPEN:
			ResponseMsg = "OPEN ERROR";
			break;

		case IoStatus.ERR_CONNECT:
			ResponseMsg = "CONNECT ERROR";
			break;

		case IoStatus.ERR_TIMEOUT:
			ResponseMsg = "TIMEOUT ERROR";
			break;

		case IoStatus.ERR_MEMORY:
			ResponseMsg = "MEMORY ERROR";
			break;

		case IoStatus.ERR_ILLEGAL:
			ResponseMsg = "ILLEGAL ERROR";
			break;

		case IoStatus.ERR_PROCESSING:
			ResponseMsg = "PROCESSING ERROR";
			break;

		default:
			ResponseMsg = "ERROR FAILURE";
			break;
		}

	}

	 private int getBuilderType() {
//	        Spinner spinner = (Spinner)findViewById(R.id.spinner_type);
	        switch(1){
	        case 1:
	            return Builder.CUT_FEED;
	        case 0:
	        default:
	            return Builder.CUT_NO_FEED;
	        }
	    }
	 
	 private int getBuilderFont() {
	        
	        switch(0){
	        case 1:
	            return Builder.FONT_B;
	        case 2:
	            return Builder.FONT_C;
	        case 0:
	        default:
	            return Builder.FONT_A;
	        }
	    }
	    
	    private int getBuilderAlign() {
	    
	        switch(0){
	        case 1:
	            return Builder.ALIGN_CENTER;
	        case 2:
	            return Builder.ALIGN_RIGHT;
	        case 0:
	        default:
	            return Builder.ALIGN_LEFT;
	        }
	    }
	    
	    private int getBuilderLineSpace() {
//	        TextView text = (TextView)findViewById(R.id.editText_linespace);
//	        try{
//	            return Integer.parseInt(text.getText().toString());
//	        }catch(Exception e){
//	            return 0;
//	        }
	    	
	    	return lineSpacing;
	    }

	    private int getBuilderLanguage() {
	        switch(0){
	        case 1:
	            return Builder.LANG_JA;
	        case 0:
	        default:
	            return Builder.LANG_EN;
	        }
	    }
	    
	    private int getBuilderSizeW() {
//	        Spinner spinner = (Spinner)findViewById(R.id.spinner_size_width);
//	        return spinner.getSelectedItemPosition() + 1;
	    	return 1;
	    }
	        
	    private int getBuilderSizeH() {
//	        Spinner spinner = (Spinner)findViewById(R.id.spinner_size_height);
//	        return spinner.getSelectedItemPosition() + 1;
	    	return 1;
	    }

	    private int getBuilderStyleBold() {
//	        ToggleButton toggle = (ToggleButton)findViewById(R.id.toggleButton_style_bold);
//	        if(toggle.isChecked()){
//	            return Builder.TRUE;
//	        }else{
//	            return Builder.FALSE;
//	        }
	    	return Builder.FALSE;
	    }

	    private int getBuilderStyleUnderline() {
//	        ToggleButton toggle = (ToggleButton)findViewById(R.id.toggleButton_style_underline);
//	        if(toggle.isChecked()){
//	            return Builder.TRUE;
//	        }else{
//	            return Builder.FALSE;
//	        }
	    	return Builder.FALSE;
	    }
	    
	    private int getBuilderXPosition() {
//	        TextView text = (TextView)findViewById(R.id.editText_xposition);
//	        try{
//	            return Integer.parseInt(text.getText().toString());
//	        }catch(Exception e){
//	            return 0;
//	        }
	    	return 0;
	    }
	    
	   

	    private int getBuilderFeedUnit() {
//	        TextView text = (TextView)findViewById(R.id.editText_feedunit);
//	        try{
//	            return Integer.parseInt(text.getText().toString());
//	        }catch(Exception e){
//	            return 0;
//	        }
	    	return 30;
	    }

}
